﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace States
{
    public class MainMenuState : BaseState
    {
        private static MainMenuState theMainMenuState;
        Texture2D Background;
        Texture2D PlayImage, CreditsImage, ExitImage;
        Vector2 PlayImagePosition, CreditsImagePosition, ExitImagePosition;
        GamePadState oldGamePad;
        int SelectedOption = 1;
        Color SelectedColor;
        float ColorMod = 0;
        private MainMenuState()
        {
            theSpriteBatch = RoyalRumble.MainGame.spriteBatch;
            theContentManager = RoyalRumble.MainGame.theContent;
            PlayImagePosition = new Vector2(160, 450);
            CreditsImagePosition = new Vector2(160, 590);
            ExitImagePosition = new Vector2(160, 730);
            SelectedColor = Color.White;
        }

        public static MainMenuState Instance
        {
            get
            {
                if (theMainMenuState == null)
                    theMainMenuState = new MainMenuState();
                return theMainMenuState;
            }
            
        }

        public override void Enter()
        {
            Background = theContentManager.Load<Texture2D>("MainMenu/MainMenuBG");
            PlayImage = theContentManager.Load<Texture2D>("MainMenu/PlayImage");
            CreditsImage = theContentManager.Load<Texture2D>("MainMenu/CreditsImage");
            ExitImage = theContentManager.Load<Texture2D>("MainMenu/ExitImage");
            
            oldGamePad = GamePad.GetState(PlayerIndex.One);
       
        }
        public void LoadGameTextures()
        {
            
        }
        public override void Exit()
        {
            Background = null;
            PlayImage = null;
            CreditsImage = null;
            ExitImage = null;
        }

        public override void GetInput(float ElapsedTime)
        {
            GamePadState theGamePad = GamePad.GetState(PlayerIndex.One);

            if (theGamePad.IsButtonDown(Buttons.LeftThumbstickUp))
            {
                if (!oldGamePad.IsButtonDown(Buttons.LeftThumbstickUp))
                {
                    SelectedOption -= 1;
                    Systems.SoundManager.Instance.PlaySound("MenuHover");
                }
            }

            if (theGamePad.IsButtonDown(Buttons.LeftThumbstickDown))
            {
                if (!oldGamePad.IsButtonDown(Buttons.LeftThumbstickDown))
                {
                    SelectedOption += 1;
                    Systems.SoundManager.Instance.PlaySound("MenuHover");
                }
            }

            if (theGamePad.IsButtonDown(Buttons.A))
            {
                if (!oldGamePad.IsButtonDown(Buttons.A))
                {
                    Systems.SoundManager.Instance.PlaySound("MenuClick");
                    RoyalRumble.MainGame.ChangeState(States.MultiplayerSelectionState.Instance);
                }
            }

            if (SelectedOption > 3)
                SelectedOption = 1;
            if (SelectedOption < 1)
                SelectedOption = 3;

            oldGamePad = theGamePad;
        }

        public override void Update(float ElapsedTime)
        {
            GetInput(ElapsedTime);

            ColorMod += 0.9f * ElapsedTime;
            if (ColorMod >= 1)
                ColorMod = 0.5f;


            SelectedColor = new Color(ColorMod, ColorMod, ColorMod, ColorMod);
            
        }
        public override void Render(float ElapsedTime)
        {
            theDevice.Clear(Color.White);
            theSpriteBatch.Begin();
            theSpriteBatch.Draw(Background, new Vector2(0, 0), Color.White);

            theSpriteBatch.Draw(PlayImage, PlayImagePosition, SelectedOption == 1? SelectedColor: Color.White);
            theSpriteBatch.Draw(CreditsImage, CreditsImagePosition, SelectedOption == 2 ? SelectedColor : Color.White);
            theSpriteBatch.Draw(ExitImage, ExitImagePosition, SelectedOption == 3 ? SelectedColor : Color.White);
            theSpriteBatch.End();
        }
    }
}